package cn.hl.hm.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.hl.hm.constants.DriverStatus;
import cn.hl.hm.constants.ModelConfig;
import cn.hl.hm.dao.GiftInfoMapper;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.GiftInfo;
import cn.hl.hm.entity.Route;
import cn.hl.hm.entity.RouteVehiclePrice;
import cn.hl.hm.entity.VehicleType;
import cn.hl.hm.entity.VtVl;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.MD5Util;
import cn.hl.hm.util.NumberUtils;
import cn.hl.hm.util.QRCodeUtil;
import cn.hl.hm.util.Result;
import cn.hl.hm.util.TokenProcessor;

/**
 * 甲方 一方奖励
 * @author LBJ
 *
 */
@Controller
@RequestMapping("/giftController")
public class GiftController {
	private final static SimpleDateFormat hhmmSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

	
	
	@Autowired
	GiftInfoMapper giMapper;
	@RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
	public String addOrUpdate(GiftInfo gi,  HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		logger.info("甲方乙方补贴规则---添加-修改---giftController/addOrUpdate/");
		request.setCharacterEncoding("UTF-8");
		try {
			 
			GiftInfo g = new GiftInfo();
			g.setStatus("1");
			List<GiftInfo> giList = giMapper.selectGiftInfo(g);
			if(giList!=null && giList.size()>0){
				GiftInfo gg = giList.get(0);
				gg.setStatus("0");
				gg.setEndTime(hhmmSdf.format(new Date()));
				int update = giMapper.updateByPrimaryKeySelective(gg);
				if (update > 0) {
					gi.setStartTime(hhmmSdf.format(new Date()));
					int add = giMapper.insertSelective(gi);
					if (add > 0) {
						Result.ok(null, InfoCode.SUCCESS, response);
						// Result.ok(record, InfoCode.SUCCESS, response);
					} else {
						Result.ok(null, InfoCode.FAILURE, response);
					}
					
					Result.ok(null, InfoCode.SUCCESS, response);
					// Result.ok(record, InfoCode.SUCCESS, response);
				} else {
					Result.ok(null, InfoCode.FAILURE, response);
				}
			}else{
				gi.setStartTime(hhmmSdf.format(new Date()));
				int add = giMapper.insertSelective(gi);
				if (add > 0) {
					Result.ok(null, InfoCode.SUCCESS, response);
					// Result.ok(record, InfoCode.SUCCESS, response);
				} else {
					Result.ok(null, InfoCode.FAILURE, response);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
	
	
	@RequestMapping(value = "/selectInfo", method = RequestMethod.POST)
	public String selectInfo(GiftInfo gi,  HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		logger.info("甲方乙方补贴规则---查询补贴规则---giftController/selectInfo");
		request.setCharacterEncoding("UTF-8");
		try {
			GiftInfo g = new GiftInfo();
//			g.setStatus("1");
			List<GiftInfo> giList = giMapper.selectGiftInfo(gi);
			if(giList!=null && giList.size()>0){
					Result.ok(giList, InfoCode.SUCCESS, response);
					// Result.ok(record, InfoCode.SUCCESS, response);
			}else{
				 
					Result.ok(null, InfoCode.SUCCESS, response);
					// Result.ok(record, InfoCode.SUCCESS, response);
				 
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}

}
